// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property
//
// Remember that both are case sensitive!


/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />


Type.registerNamespace('ReserveSocietyControls');

ReserveSocietyControls.TextBoxWaterMarkBehavior = function(element) {
    ReserveSocietyControls.TextBoxWaterMarkBehavior.initializeBase(this, [element]);

    this._Text = null;
    this._WaterMarkCssClass = null;
}

ReserveSocietyControls.TextBoxWaterMarkBehavior.prototype = {
    initialize : function() 
    {
        ReserveSocietyControls.TextBoxWaterMarkBehavior.callBaseMethod(this, 'initialize');
            $addHandler(this.get_element(), 'focus',
                    Function.createDelegate(this, this._onfocus));
            $addHandler(this.get_element(), 'blur',
                    Function.createDelegate(this, this._onblur));

        var hasInitialFocus = false;
        var clientState = ReserveSocietyControls.TextBoxWaterMarkBehavior.callBaseMethod(this, 'get_ClientState');
        if (clientState != null && clientState != "") {
            hasInitialFocus = (clientState == "Focused");
            AjaxControlToolkit.TextBoxWatermarkBehavior.callBaseMethod(this, 'set_ClientState', null);
        }
        
        if(hasInitialFocus)
        {
            this._onfocus();
        }
        else
        {
            this._onblur();
        }
    },

    dispose : function() {
        // TODO: Add your cleanup code here

        ReserveSocietyControls.TextBoxWaterMarkBehavior.callBaseMethod(this, 'dispose');
    },
    
    _onfocus : function()
    {
        var elem = this.get_element();
        if(elem.value == this.get_Text())
        {
            elem.value = "";
        }
    },

    _onblur : function()
    {
        var elem = this.get_element();
        if(elem.value == "")
        {
            elem.value = this.get_Text();
        }
    },
    
    _onSubmit : function()
    {
        var elem = this.get_element();
        alert(elem);
        if(elem.value == this.get_Text())
        {
            elem.value = "";
        }
    },

    get_Text : function() {
        return this._Text;
    },

    set_Text : function(value) {
        this._Text = value;
    },
    
    get_WaterMarkCssClass : function() {
        return this._WaterMarkCssClass;
    },

    set_WaterMarkCssClass : function(value) {
        this._WaterMarkCssClass = value;
    }
}
ReserveSocietyControls.TextBoxWaterMarkBehavior.registerClass('ReserveSocietyControls.TextBoxWaterMarkBehavior', AjaxControlToolkit.BehaviorBase);

ReserveSocietyControls.TextBoxWaterMarkBehavior.WebForm_OnSubmit = function() {
    /// <summary>
    /// Wraps ASP.NET's WebForm_OnSubmit in order to strip all watermarks prior to submission
    /// </summary>
    /// <returns type="Boolean">
    /// Result of original WebForm_OnSubmit
    /// </returns>
    alert("submitting");
    var result = ReserveSocietyControls.TextBoxWaterMarkBehavior._originalWebForm_OnSubmit();
    if (result) {
        var components = Sys.Application.getComponents();
        for(var i = 0 ; i < components.length ; i++) {
            var component = components[i];
            if (ReserveSocietyControls.TextBoxWaterMarkBehavior.isInstanceOfType(component)) {
                component._onSubmit();
            }
        }
    }
    return result;
  }
